<?php
#changelog
#09-05-14 :修正appeal_get_appeal, appeal_get_list没有返回telphone的错误;
#09-05-12 :调整appeal_add_appeal,appeal_get_appeal, appeal_get_list;：分别加入两个字段
#09-05-04 :设置协议超时时间 socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,1);
#09-04-27 :change user_get_userid_by_email


define( "SUCC" ,							0	);

function hex2bin_pri($hexdata) {
    $bindata = '';
    for($i=0; $i < strlen($hexdata); $i += 2) {
        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
    }
    return $bindata;
}

class proto_socket{
    //private:
    var $socket; //socket 句柄
    //var $debug = 1;
    function __construct( $payserip,$payserport){
        $address = gethostbyname($payserip );
        if (($this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP))< 0 )
        {
            trigger_error("Couldn't create socket: " . socket_strerror(socket_last_error()) . "\n");
        }
        $result = socket_connect($this->socket,$address,$payserport );
        socket_set_option($this->socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>60, "usec"=>0 ) );
        socket_set_option($this->socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>60, "usec"=>0 ) );
        #socket_connect socketfd, SOL_SOCKET, SO_RCVTIMEO,

    }
    function log_msg($msg){
        global $_SESSION;
        $log_pkg_arr=@unpack("Lproto_len/Lproto_id/Scommandid/Lresult/Luserid",$msg);
        if ( ($log_pkg_arr["commandid"] & 0x0100) ==0x0100) {
            dbg_log( $_SESSION["adminid"].":".  dechex($log_pkg_arr["commandid"] ).
                    ":".$log_pkg_arr["userid"].":".bin2hex($msg));
        }
    }

    function sendmsg($msg){
        //$this->log_msg($msg);
        socket_write($this->socket,$msg, strlen($msg) );
        $buf= socket_read($this->socket,8192);
        $pkg_arr=@unpack("Lproto_len",$buf);
        $proto_len= $pkg_arr["proto_len"];
        #echo $proto_len;
        while ($proto_len!=strlen($buf) ){
            $buf .=	socket_read($this->socket,4096);
        }
        return $buf;
    }
    function sendmsg_without_returnmsg($msg){
        //$this->log_msg($msg);
        $result=socket_write($this->socket,$msg, strlen($msg) );
        if ($result){
            return array("result"=>0);
        }else
            return array("result"=>1010);
    }

    function close(){
        socket_close($this->socket);
    }
    function __destruct(){
    }
}


class proto{
    var $sock;
    var $adminid;
    function __construct( $payserip,$payserport){
        $this->sock = new proto_socket ($payserip,$payserport);
    }
    function __destruct(){
        if ($this->sock) $this->sock->close();
    }

    function set_adminid($adminid){
        $this->adminid=$adminid;
    }


    function park($cmdid,$userid,$private_msg){
        global $_SESSION;
        //18：报文头部长度
        $pkg_len=18+strlen($private_msg) ;
        $result=0;
        $proto_id=$this->adminid;
        if ($proto_id==0){
            $proto_id=$_SESSION["adminid" ];
        }
        return pack("L2SL2",$pkg_len,$proto_id,hexdec($cmdid),$result,$userid)
            .$private_msg;
    }

    function unpark($sockpkg, $private_fmt){
        //echo bin2hex($sockpkg);
        $pkg_arr=@unpack("Lproto_len/Lproto_id/Scommandid/Lresult/Luserid",$sockpkg);
        //$tmp_len=$pkg_arr["proto_len"];
        if ($private_fmt!="" && $pkg_arr["result"]==0){//成功
            $pkg_arr=@unpack("Lproto_len/Lproto_id/Scommandid/Lresult/Luserid/".$private_fmt,
                    $sockpkg);
        }
        if ($pkg_arr){
            return $pkg_arr;
        }else{
            return array("result"=>1003 );
        }
    }

    function send_cmd($cmdid ,$userid, $pri_msg , $out_msg ){
        $sendbuf=$this->park( $cmdid , $userid,  $pri_msg );
        return $this->unpark($this->sock->sendmsg($sendbuf),$out_msg );
    }

    //获取用户是否已入住游戏
    function userinfo_get_gameflag($userid){
        $cmdid="0007";
        $in_msg="";
        $out_msg="Lgameflag";
        return  $this->send_cmd($cmdid,$userid,$in_msg,$out_msg);
    }

    function user_get_userid_by_email($email){
        $pri_msg=pack("A64", $email );
        $sendbuf=$this->park(4004,0, $pri_msg);
        return $this->unpark($this->sock->sendmsg($sendbuf),"Luserid");
    }

   
